﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace SilverlightBase
{
	public static class Stuff
	{
		public static int ToInt32(this string s)
		{
			return Convert.ToInt32(s);
		}

		public static string StripHtml(this string s)
		{
			return s.Replace("<p>\n", "").Replace("</p>", "\r");
		}


		/// <summary>
		/// Converts an hex colour string to a byte array
		/// </summary>
		/// <param name="hexString">hex color</param>
		/// <returns>byte[] {255, 255, 255}</returns>
		public static byte[] HexToData(string hexString)
		{
			try
			{
				if (hexString.Length % 2 == 1)
					hexString = '0' + hexString;

				byte[] data = new byte[hexString.Length / 2];

				for (int i = 0; i < data.Length; i++)
					data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

				return data;
			}
			catch
			{
				// return default colour
				return new byte[] { 0, 174, 164 };
			}
		}

		public static bool IsEmail(string email)
		{
			string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
			Regex re = new Regex(strRegex);

			return re.IsMatch(email);
		}
	}
}
